﻿using FirstFloor.ModernUI.Presentation;
using SelElectCalcModernUI.Lib;
//using Microsoft.Scripting.Hosting;
using SelElectCalcModernUI.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SelElectCalcModernUI.ViewModel.CalcRes
{
    public class AllDealViewModel
        : NotifyPropertyChanged, IDisposable
    {
        CommandBase exportCmd;
        CommandBase returnCmd;
        CommandBase allExportCmd;
        ObservableCollection<DealRes> dealData;

        public CommandBase ExportCmd
        {
            get
            {
                if (exportCmd == null)
                    exportCmd = new RelayCommand((s) =>
                    {
                        SaveFile();
                    }, (s) => dealData != null && dealData.Count > 0);
                return exportCmd;
            }
        }
        public CommandBase ReturnCmd
        {
            get
            {
                if (returnCmd == null)
                    returnCmd = new RelayCommand((s) =>
                    {
                        ViewModelLocator.JumpPageEvent("/Pages/HomePage.xaml");
                    });
                return returnCmd;
            }
        }
        public CommandBase AllExportCmd
        {
            get
            {
                if (allExportCmd == null)
                    allExportCmd = new RelayCommand((s) =>
                    {
                        try
                        {
                            var stream = ViewModelLocator.SaveFileEvent("csv");
                            if (stream == null)
                                return;
                            System.IO.StreamWriter tr = new System.IO.StreamWriter(stream, Encoding.Default);
                            SaveFileString(tr);
                            ViewModelLocator.FactoryResult.SaveFileString(tr);
                            ViewModelLocator.SellerResult.SaveFileString(tr);
                            tr.Flush();
                            tr.Close();
                            System.Windows.MessageBox.Show("OK");
                        }
                        catch (Exception ex)
                        {
                            System.Windows.MessageBox.Show("Error:" + ex.ToString());
                        }
                    });
                return allExportCmd;
            }
        }
        public ObservableCollection<DealRes> DealData
        {
            get
            {
                if (dealData == null)
                    dealData = new ObservableCollection<Model.DealRes>();
                return dealData;
            }
        }
        public IEnumerable<ChartModel> DealChartData
        {
            get
            {
                OnPropertyChanged("YMax");
                OnPropertyChanged("YMin");
                return from c in dealData
                       select new ChartModel()
                       {
                           Name = c.Factory_name + " " + c.Factory_time + "\r\n"
 + c.Seller_name + " " + c.Seller_time,
                           Value = new double[4] { c.Factory_price, c.Seller_price, c.Factory_price, c.Seller_price },
                           DisTip = c.Factory_name + " " + c.Factory_time + ":" + c.Factory_price + "元\r\n" +
  c.Seller_name + " " + c.Seller_time + " " + c.Seller_price + "元",
                       };
            }
        }
        public double YMax
        {
            get
            {
                return Math.Max(dealData.Max(c => c.Factory_price), dealData.Max(c => c.Seller_price));
            }
        }
        public double YMin
        {
            get
            {
                return Math.Min(dealData.Min(c => c.Factory_price), dealData.Min(c => c.Seller_price));
            }
        }

        public void SetDealData(IEnumerable<DealRes> data)
        {
            DealData.Clear();
            foreach (var item in data)
                DealData.Add(item);
            OnPropertyChanged("DealChartData");
        }

        private void SaveFile()
        {
            var stream = ViewModelLocator.SaveFileEvent("csv");
            if (stream == null)
                return;
            System.IO.StreamWriter tr = new System.IO.StreamWriter(stream, Encoding.Default);
            SaveFileString(tr);
            tr.Flush();
            tr.Close();
        }

        private void SaveFileString(System.IO.StreamWriter tr)
        {
            tr.WriteLine("实际成交价差排序(厘/Kwh),实际成交序号,本序成交供方名称,本序供方总可售电量(万Kwh),本序供方中标售电量(万Kwh),"
                + "本序供方报价(厘/Kwh),本序供方剩余可售电量(万Kwh),本序成交需方名称,本序需方总可购电量(万Kwh),本序需方中标购电量(万Kwh),"
                + "本序需方报价(厘/Kwh),本序需方剩余可购电量(万Kwh),本序后市场累计成交量(万Kwh)");
            foreach (var se in dealData)
            {
                tr.WriteLine((se.Diff_price * 1000).GetString()
                    + "," + se.Index
                    + "," + se.Factory_name
                    + "," + se.Factory_this_power.GetString()
                    + "," + se.Del_Power.GetString()
                    + "," + (se.Factory_price * 1000).GetString()
                    + "," + se.Factory_left_power.GetString()
                    + "," + se.Seller_name
                    + "," + se.Seller_this_power.GetString()
                    + "," + se.Del_Power.GetString()
                    + "," + (se.Seller_price * 1000).GetString()
                    + "," + se.Seller_left_power.GetString()
                    + "," + se.Tot_power.GetString());
            }
        }

        public void Dispose()
        {
        }
    }

}
